home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / midi / midifile.lha / Midifile / mftext.c < prev    next >
C/C++ Source or Header  |  1995-08-13  |  5KB  |  273 lines

  1. /*
  2.  * mftext
  3.  * 
  4.  * Convert a MIDI file to verbose text.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "midifile.h"
  10.  
  11. static FILE *F;
  12. int SECONDS;            /* global that tells whether to display seconds or ticks */
  13. int division;            /* from the file header */
  14. long tempo = 500000;        /* the default tempo is 120 beats/minute */
  15.  
  16. filegetc ()
  17. {
  18.   return (getc (F));
  19. }
  20.  
  21. /* for crack */
  22. extern int arg_index;
  23.  
  24. main (argc, argv)
  25.      char **argv;
  26. {
  27.   FILE *efopen ();
  28.   char ch;
  29.  
  30.   SECONDS = 0;
  31.  
  32.   while ((ch = crack (argc, argv, "s", 0)) != NULL)
  33.     {
  34.       switch (ch)
  35.     {
  36.     case 's':
  37.       SECONDS = 1;
  38.       break;
  39.     }
  40.     }
  41.  
  42.   if (argc < 2 && !SECONDS || argc < 3 && SECONDS)
  43.     F = stdin;
  44.   else
  45.     F = efopen (argv[arg_index], "r");
  46.  
  47.   initfuncs ();
  48.   Mf_getc = filegetc;
  49.   midifile ();
  50.   fclose (F);
  51.   exit (0);
  52. }
  53.  
  54. FILE *
  55. efopen (name, mode)
  56.      char *name;
  57.      char *mode;
  58. {
  59.   FILE *f;
  60.   extern int errno;
  61.  
  62.  
  63.   char *errmess;
  64.  
  65.   if ((f = fopen (name, mode)) == NULL)
  66.     {
  67.       (void) fprintf (stderr, "*** ERROR *** Cannot open '%s'!\n", name);
  68.       exit (1);
  69.     }
  70.   return (f);
  71. }
  72.  
  73. error (s)
  74.      char *s;
  75. {
  76.   fprintf (stderr, "Error: %s\n", s);
  77. }
  78.  
  79. txt_header (format, ntrks, ldivision)
  80. {
  81.   division = ldivision;
  82.   printf ("Header format=%d ntrks=%d division=%d\n", format, ntrks, division);
  83. }
  84.  
  85. txt_trackstart ()
  86. {
  87.   printf ("Track start\n");
  88. }
  89.  
  90. txt_trackend ()
  91. {
  92.   printf ("Track end\n");
  93. }
  94.  
  95. txt_noteon (chan, pitch, vol)
  96. {
  97.   prtime ();
  98.   printf ("Note on, chan=%d pitch=%d vol=%d\n", chan + 1, pitch, vol);
  99. }
  100.  
  101. txt_noteoff (chan, pitch, vol)
  102. {
  103.   prtime ();
  104.   printf ("Note off, chan=%d pitch=%d vol=%d\n", chan + 1, pitch, vol);
  105. }
  106.  
  107. txt_pressure (chan, pitch, press)
  108. {
  109.   prtime ();
  110.   printf ("Pressure, chan=%d pitch=%d press=%d\n", chan + 1, pitch, press);
  111. }
  112.  
  113. txt_parameter (chan, control, value)
  114. {
  115.   prtime ();
  116.   printf ("Parameter, chan=%d c1=%d c2=%d\n", chan + 1, control, value);
  117. }
  118.  
  119. txt_pitchbend (chan, msb, lsb)
  120. {
  121.   prtime ();
  122.   printf ("Pitchbend, chan=%d msb=%d lsb=%d\n", chan + 1, msb, lsb);
  123. }
  124.  
  125. txt_program (chan, program)
  126. {
  127.   prtime ();
  128.   printf ("Program, chan=%d program=%d\n", chan + 1, program);
  129. }
  130.  
  131. txt_chanpressure (chan, press)
  132. {
  133.   prtime ();
  134.   printf ("Channel pressure, chan=%d pressure=%d\n", chan + 1, press);
  135. }
  136.  
  137. txt_sysex (leng, mess)
  138.      char *mess;
  139. {
  140.   prtime ();
  141.   printf ("Sysex, leng=%d\n", leng);
  142. }
  143.  
  144. txt_metamisc (type, leng, mess)
  145.      char *mess;
  146. {
  147.   prtime ();
  148.   printf ("Meta event, unrecognized, type=0x%02x leng=%d\n", type, leng);
  149. }
  150.  
  151. txt_metaspecial (type, leng, mess)
  152.      char *mess;
  153. {
  154.   prtime ();
  155.   printf ("Meta event, sequencer-specific, type=0x%02x leng=%d\n", type, leng);
  156. }
  157.  
  158. txt_metatext (type, leng, mess)
  159.      char *mess;
  160. {
  161.   static char *ttype[] =
  162.   {
  163.     NULL,
  164.     "Text Event",        /* type=0x01 */
  165.     "Copyright Notice",        /* type=0x02 */
  166.     "Sequence/Track Name",
  167.     "Instrument Name",        /* ...       */
  168.     "Lyric",
  169.     "Marker",
  170.     "Cue Point",        /* type=0x07 */
  171.     "Unrecognized"
  172.   };
  173.   int unrecognized = (sizeof (ttype) / sizeof (char *)) - 1;
  174.   register int n, c;
  175.   register char *p = mess;
  176.  
  177.   if (type < 1 || type > unrecognized)
  178.     type = unrecognized;
  179.   prtime ();
  180.   printf ("Meta Text, type=0x%02x (%s)  leng=%d\n", type, ttype[type], leng);
  181.   printf ("     Text = <");
  182.   for (n = 0; n < leng; n++)
  183.     {
  184.       c = *p++;
  185.       printf ((isprint (c) || isspace (c)) ? "%c" : "\\0x%02x", c);
  186.     }
  187.   printf (">\n");
  188. }
  189.  
  190. txt_metaseq (num)
  191. {
  192.   prtime ();
  193.   printf ("Meta event, sequence number = %d\n", num);
  194. }
  195.  
  196. txt_metaeot ()
  197. {
  198.   prtime ();
  199.   printf ("Meta event, end of track\n");
  200. }
  201.  
  202. txt_keysig (sf, mi)
  203. {
  204.   prtime ();
  205.   printf ("Key signature, sharp/flats=%d  minor=%d\n", sf, mi);
  206. }
  207.  
  208. txt_tempo (ltempo)
  209.      long ltempo;
  210. {
  211.   tempo = ltempo;
  212.   prtime ();
  213.   printf ("Tempo, microseconds-per-MIDI-quarter-note=%d\n", tempo);
  214. }
  215.  
  216. txt_timesig (nn, dd, cc, bb)
  217. {
  218.   int denom = 1;
  219.   while (dd-- > 0)
  220.     denom *= 2;
  221.   prtime ();
  222.   printf ("Time signature=%d/%d  MIDI-clocks/click=%d  32nd-notes/24-MIDI-clocks=%d\n",
  223.       nn, denom, cc, bb);
  224. }
  225.  
  226. txt_smpte (hr, mn, se, fr, ff)
  227. {
  228.   prtime ();
  229.   printf ("SMPTE, hour=%d minute=%d second=%d frame=%d fract-frame=%d\n",
  230.       hr, mn, se, fr, ff);
  231. }
  232.  
  233. txt_arbitrary (leng, mess)
  234.      char *mess;
  235. {
  236.   prtime ();
  237.   printf ("Arbitrary bytes, leng=%d\n", leng);
  238. }
  239.  
  240. prtime ()
  241. {
  242.   if (SECONDS)
  243.     printf ("Time=%f   ", mf_ticks2sec (Mf_currtime, division, tempo));
  244.   else
  245.     printf ("Time=%ld  ", Mf_currtime);
  246. }
  247.  
  248. initfuncs ()
  249. {
  250.   Mf_error = error;
  251.   Mf_header = txt_header;
  252.   Mf_trackstart = txt_trackstart;
  253.   Mf_trackend = txt_trackend;
  254.   Mf_noteon = txt_noteon;
  255.   Mf_noteoff = txt_noteoff;
  256.   Mf_pressure = txt_pressure;
  257.   Mf_parameter = txt_parameter;
  258.   Mf_pitchbend = txt_pitchbend;
  259.   Mf_program = txt_program;
  260.   Mf_chanpressure = txt_chanpressure;
  261.   Mf_sysex = txt_sysex;
  262.   Mf_metamisc = txt_metamisc;
  263.   Mf_seqnum = txt_metaseq;
  264.   Mf_eot = txt_metaeot;
  265.   Mf_timesig = txt_timesig;
  266.   Mf_smpte = txt_smpte;
  267.   Mf_tempo = txt_tempo;
  268.   Mf_keysig = txt_keysig;
  269.   Mf_seqspecific = txt_metaspecial;
  270.   Mf_text = txt_metatext;
  271.   Mf_arbitrary = txt_arbitrary;
  272. }
  273.